$(function(){
	$('[validator]').blur(function(){
		$(this).validate();
	});
});

jQuery.fn.extend({
	validate:function(){
		var me = this,
			vlds = me.find('[validator]'),
			validated=true;
		//递归调用validate
		if(vlds && vlds.length>0){  //遍历所有子元素
			for(var i=0,len=vlds.length;i<len;i++){
				var r = $(vlds[i]).validate();
				if(!r.result)
					validated=false;
			}
			return validated;
		}else if(me.attr('validator')){
			var vdstr = me.attr('validator');
			if(!vdstr)
				return {result:true};
			var vds = vdstr.split('|'),
				len = vds.length;
			
			//处理单字段多个校验方法
			for(var i=0;i<len;i++){
				var vd = vds[i];
					ind1 = vd.indexOf("("),
					vn = vd,
					fun="",
					params="",
					v = me.val();
					
				if(v==null || v==undefined)
					v='';
				if(ind1>0){
					vn = vd.substr(0,ind1);
					params = vd.substring(ind1+1,vd.length-1);
				}
				fun += "$jValidator.valid('" + vn + "','" + v + "'";
				
				if(params)
					fun += "," + params
				fun += ")";
				var r = eval(fun);
				if(!r.result){
					if(r.msg){
						try{
							me.tooltip('destroy');
						}catch(e){}
						var tip = me.tooltip({
							title:r.msg,
							placement:'right',
							trigger:'manual'
						});
						
						me.tooltip('show');
						
						me.parent().addClass('has-error');
					}else{
						me.tooltip('destroy');
						me.parent().removeClass('has-error');
					}
					return r;
				}else{
					me.tooltip('destroy');
					me.parent().removeClass('has-error');
				}
			}
			return {result:true};
		}
	},
	addTooltip: function(text) {
		if(!text)
			text = "请完善信息";
		var me = $(this);
		var next = me.next();
		if(!next.length || next[0].className.indexOf('tooltip-my') == -1) {
			var html_next = '<div class="tooltip-my">'
				+ '<div class="tooltip-my-arrow"></div>'
				+ '<div class="tooltip-my-inner">'
				+ text
				+ '</div>'
				+ '<div style="clear: both;"></div>'
				+ '</div>';
			next = $(html_next);
			next.insertAfter(me);
		}
		next.show();
		me.parent().addClass('has-error');
	},
	removeTooltip: function() {
		var next = $(this).next();
		if(next.length && next[0].className.indexOf('tooltip-my') > -1) {
			next.remove();
		}
		$(this).parent().removeClass('has-error');
	}
});

/**
 * datas: [{}]
 */
function validateTooltip(datas) {
	var valid = true;
	$.each(datas, function(i, data) {
		var $input = $(data.inputSel);
		var vdstr = data.validStr;
		var $tip = $(data.tipSel);
		if(!$tip || !$tip.length)
			$tip = $input;
		var vds = vdstr.split('|'),
		len = vds.length;
		for(var i=0;i<len;i++){
			var vd = vds[i];
				ind1 = vd.indexOf("("),
				vn = vd,
				fun="",
				params="",
				v = $input.val();
				
			if(v==null || v==undefined)
				v='';
			if(ind1>0){
				vn = vd.substr(0,ind1);
				params = vd.substring(ind1+1,vd.length-1);
			}
			fun += "$jValidator.valid('" + vn + "','" + v + "'";
			if(params)
				fun += "," + params
			fun += ")";
			var r = eval(fun);
			$tip.removeTooltip();
			if(r && !r.result && r.msg){
				valid = false;
				$tip.addTooltip(r.msg);
			}
		}
	});
	return valid;
}